<?php
namespace Profiles\Controller;
use Hybrid_Auth;
use ScnSocialAuth\Mapper\Exception as MapperException;
use ScnSocialAuth\Mapper\UserProviderInterface;
use ScnSocialAuth\Options\ModuleOptions;
use Zend\View\Model\ModelInterface;
use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;
use Users\Model\User;
use Users\Model\Usernew;
use Profiles\Model\Comment;
use Application\Model\Cities;          // <-- Add this import
use Users\Form\UserForm; 
use Zend\View\Model\JsonModel;
use Zend\Session\Container;
use Users\Model\Forgetpassword;
use Application\Model\country;
use Application\Model\state;
use Users\Model\Flavor;
use Products\Model\Productsnew;
use Products\Model\Bid;
class profilesController extends AbstractActionController
{

	protected  $adminCombinationsTable;
	protected  $userRegionsTable;
	protected  $commentTable;
	
	  /**
     * @var UserProviderInterface
     */
    protected $mapper;

    /**
     * @var Hybrid_Auth
     */
    protected $hybridAuth;

    /**
     * @var ModuleOptions
     */
    protected $options;

    /*
     * @todo Make this dynamic / translation-friendly
     * @var string
     */
    protected $failedAddProviderMessage = 'Add provider failed. Please try again.';
	public function setOptions(ModuleOptions $options)
    {
        $this->options = $options;

        return $this;
    }
	public function staticAction()
	{
    $pageName = $this->params('page_name');
	
    $view = new ViewModel();
    $view->setTemplate('static/' . $pageName); 

    return $view;
	}
    public function indexAction()
	{
		
	}
	public function userprofileAction()
	{
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!="")
		{
			$user_id = $_SESSION['user']['user_id'];
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser( $user_id );
			 $existingRegionsCount = $this->getUserRegionsTable()->getRegionsCount( $user_id );
			 $loadedRegions = $this->getUserRegionsTable()->loadRegions( $user_id );
		}
		else
		{
			echo "No user id.";
		}

		return new ViewModel(array(
		'user_details' 	=> $getdetails,
		'existingRegionsCount' 	=> $existingRegionsCount,
		'loadedRegions' 	=> $loadedRegions,
		));
	}
	public function userprofilenextAction()
	{
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!="")
		{
			$user_id = $_SESSION['user']['user_id'];
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser( $user_id );
		}
		else
		{
			echo "No user id.";
		}

		return new ViewModel(array(
		'user_details' 	=> $getdetails,
		));
	}
	public function userprofileconfirmAction()
	{
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!="")
		{
			$user_id = $_SESSION['user']['user_id'];
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser( $user_id );
			 $existingRegionsCount = $this->getUserRegionsTable()->getRegionsCount( $user_id );
			 $loadedRegions = $this->getUserRegionsTable()->loadRegions( $user_id );
		}
		else
		{
			echo "No user id.";
		}

		return new ViewModel(array(
		'user_details' 	=> $getdetails,
		'existingRegionsCount' 	=> $existingRegionsCount,
		'loadedRegions' 	=> $loadedRegions,
		));
	}	

	public function adminProfileAction()
	{
		// echo "adminProfileAction" . "<br/>";
		// exit;
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!="")
		{
			$user_id = $_SESSION['user']['user_id'];
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser( $user_id );
			 $existingCombsCount = $this->getAdminCombinationsTable()->getCombsCount( $user_id );
			 $loadedCombinations = $this->getAdminCombinationsTable()->loadCombinations( $user_id );
		}
		else
		{
			echo "No user id.";
		}

		return new ViewModel(array(
		'user_details' 	=> $getdetails,
		'existingCombsCount' 	=> $existingCombsCount,
		'loadedCombinations' 	=> $loadedCombinations,
		));
	}

	public function adminProfileNextAction()
	{
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!="")
		{
			$user_id = $_SESSION['user']['user_id'];
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser( $user_id );
		}
		else
		{
			echo "No user id.";
		}

		return new ViewModel(array(
		'user_details' 	=> $getdetails,
		));
	}

	public function adminProfileConfirmAction()
	{
		if(isset($_SESSION['user']['user_id']) && $_SESSION['user']['user_id']!="")
		{
			$user_id = $_SESSION['user']['user_id'];
			 $table = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			 $getdetails = $table->editUser( $user_id );
			 $existingCombsCount = $this->getAdminCombinationsTable()->getCombsCount( $user_id );
			 $loadedCombinations = $this->getAdminCombinationsTable()->loadCombinations( $user_id );
		}
		else
		{
			echo "No user id.";
		}

		return new ViewModel(array(
		'user_details' 	=> $getdetails,
		'existingCombsCount' 	=> $existingCombsCount,
		'loadedCombinations' 	=> $loadedCombinations,
		));
	}	
	
	public function getAdminCombinationsTable()
    {
        if (!$this->adminCombinationsTable) {
            $sm = $this->getServiceLocator();
            $this->adminCombinationsTable = $sm->get('Users\Model\AdminCombinationsTable');
        }
        return $this->adminCombinationsTable;
    }		
	public function getUserRegionsTable()
    {
        if (!$this->userRegionsTable) {
            $sm = $this->getServiceLocator();
            $this->userRegionsTable = $sm->get('Users\Model\UserRegionsTable');
        }
        return $this->userRegionsTable;
    }
	public function getCommentTable()
    {
        if (!$this->commentTable) {
            $sm = $this->getServiceLocator();
            $this->commentTable = $sm->get('Profiles\Model\CommentTable');
        }
        return $this->commentTable;
    }
	public function commentAction(){
		include('public/PHPMailer_5.2.4/sendmail.php');
		global $admincommentSubject;				
		global $admincommentMessage;
		global $supercommentSubject;				
		global $supercommentMessage;
		if(isset($_POST['comment']) && $_POST['comment']!=""){
		  $addComment=$this->getCommentTable()->addCommnet($_POST);
		  $commentMessage=$_POST['comment'];
		  If( $addComment > 0 ) 
		  {	
			$user_id=$_SESSION['user']['user_id'];
			$userTable = $this->getServiceLocator()->get('Users\Model\UsersnewTable');
			$user_info= $userTable->editUser($user_id);			
			if( $user_info!="" ){
				$user_user_name = $user_info->company_name;
				$admin_id=$user_info->parent_user_id;
				$get_admin_details= $userTable->editUser($admin_id);				
				if( $get_admin_details !="" ){
					$admin_user_name = $get_admin_details->company_name;
					$admin_eamil = $get_admin_details->email;
					$super_admin_id = $get_admin_details->parent_user_id;
					$get_superadmin_details= $userTable->editUser($super_admin_id);
					$admincommentMessage = str_replace("<FULLNAME>","$admin_user_name", $admincommentMessage);
					$admincommentMessage = str_replace("<USERNAME>","$user_user_name", $admincommentMessage);
					$admincommentMessage = str_replace("<MESSAGE>","$commentMessage", $admincommentMessage);
					if($get_superadmin_details !="" ){
						$superadmin_user_name = $get_admin_details->company_name;
						$superadmin_eamil = $get_superadmin_details->email;
						$supercommentMessage = str_replace("<FULLNAME>","$superadmin_user_name", $supercommentMessage);
						$supercommentMessage = str_replace("<ADMINUSERNAME>","$admin_user_name", $supercommentMessage);
						$supercommentMessage = str_replace("<USERNAME>","$user_user_name", $supercommentMessage);
						$supercommentMessage = str_replace("<MESSAGE>","$commentMessage", $supercommentMessage);
						if(sendMail($admin_eamil,$admincommentSubject,$admincommentMessage)){
							if(sendMail($superadmin_eamil,$supercommentSubject,$supercommentMessage)){
								return $this->redirect()->toUrl('comment?success=success');
							}
						}
					}
				}
			}	
		  }
		} 
	}
}		

	
